package com.esun.payment_api.controller;

import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.domain.SysCustomer;
import com.esun.payment_api.domain.SysDictCustomer;
import com.esun.payment_api.domain.SysDictData;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.service.SysDictCustomerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.esun.common.utils.JWTUtils.getLoginUser;

@RestController
@RequestMapping("/sysDictCustomer")
public class SysDictCustomerController extends BaseController{

    @Resource
    SysDictCustomerService sysDictCustomerService;

    /**
     * 查询所有客户字典信息，条件查询
     */
    @GetMapping("/querySysDictCustomerAll")
    public CommonResult querySysDictCustomerAll(Short coId, String keyword, String word) {
        try {
            List<SysDictCustomer> list = sysDictCustomerService.selectByExample(coId, keyword, word);
            return CommonResult.success(list, "客户字典查询成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 添加单个
     */
    @RequestMapping("/addSysDictCustomerOne")
    public CommonResult addSysDictCustomerOne(@RequestBody SysDictCustomer record) {
        if (sysDictCustomerService.insertSelective(record) > 0) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败，请检查数据");
    }

    /**
     * 根据id删除单个(软删除)
     */
    @RequestMapping("/deleteSysDictCustomerById")
    public CommonResult deleteSysDictCustomerById(Short id) {
        if (sysDictCustomerService.deleteById(id) > 0) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败，请查看id是否存在");
    }

    /**
     * 根据id修改单个
     */
    @RequestMapping("/updateSysDictCustomer")
    public CommonResult updateSysDictCustomer(@RequestBody SysDictCustomer record) {
        if (sysDictCustomerService.updateByPrimaryKeySelective(record) > 0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败，请检查数据");
    }

    /**
     * 用户添加单个往来单位 - 注意，该实体类为SysCustomer.
     */
    @RequestMapping("/addSysCustomerOne")
    public CommonResult addSysDictCustomerOne(@RequestBody SysCustomer record) {
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(user.getId())) {
            return CommonResult.failed("登录人id为空");
        }
        record.setCreateBy(user.getId());
        record.setCreatedByUser((short)1);
        record.setStatus("1");
        record.setIsDeleted("0");
        record.setCoId(user.getCoId());
//        检查客户名称、编号、税号、地址+电话、开户行+银行账号是否为空以及其唯一性
        if(BlankUtils.isBlank(record.getName()) || BlankUtils.isBlank(record.getCode()) || BlankUtils.isBlank(record.getItin()) || BlankUtils.isBlank(record.getAddress()) || BlankUtils.isBlank(record.getOpeningBank())){
            return CommonResult.failed("客户名称、编号、税号、地址+电话、开户行+银行账号不可为空");
        }
        if(sysDictCustomerService.getSysCustomerByName(record.getName()).size()!=0){
            return CommonResult.failed("客户名称必须是唯一的");
        }
        if(sysDictCustomerService.getSysCustomerByCode(record.getCode()).size()!=0){
            return CommonResult.failed("客户编号必须是唯一的");
        }
        if(sysDictCustomerService.getSysCustomerByItin(record.getItin()).size()!=0){
            return CommonResult.failed("客户税号必须是唯一的");
        }

        if (sysDictCustomerService.insertSysCustomerSelective(record) > 0) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败，请检查数据");
    }

    /**
     * 获取往来单位类型字典
     */
    @RequestMapping("/getCustomerTypeDict")
    public CommonResult getCustomerTypeDict() {
        try{
            User user = getLoginUser();
            List<SysDictData> list = sysDictCustomerService.getCustomerTypeDict(user.getCoId());
            return CommonResult.success(list,"往来单位类型字典读取成功");
        }catch (Exception e){
            return CommonResult.failed("往来单位类型字典读取失败");
        }

    }

}
